﻿using MediatR;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;


namespace Production_Get.API.Application.Commands.Procurement
{
    public class SupplierShowHandler : IRequestHandler<SupplierShowCommand, Result>
    {
        private readonly IBaseRepositoryGet<Suppliers> suppliersrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SupplierShowHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="suppliersrepository"></param>
        /// <param name="logger"></param>
        public SupplierShowHandler(IBaseRepositoryGet<Suppliers> suppliersrepository, ILogger<SupplierShowHandler> logger)
        {
            this.suppliersrepository = suppliersrepository;
            this.logger = logger;

        }

        public async Task<Result> Handle(SupplierShowCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM Suppliers";
            try
            {
                logger.LogInformation("获取供应商数据");
                var list = suppliersrepository.GetAll(sql, default).ToList();
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "显示成功";
                }

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("获取供应商数据失败", ex.Message);
                throw;
            }
        }
    }
}
